﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ALDIOrganizer
{

    public class DataGridViewOpeningHrs : DataGridViewPopup 
    {
        public SheetView DgridTemplate { get; private set; }

        private Template _template;
        
        public void Bind(Template pTemplate, SheetView pDataGridViewTemplate)
        {
            _template = pTemplate;
            DataSource = null; // to clear up
            Columns.Clear();            

            var column = new DataGridViewButtonColumn {DataPropertyName = "add"};
            Columns.Add(column);
            for (var d = 1; d < 8; d++)
            {
                var btc = new DataGridViewButtonColumn
                              {
                                  DataPropertyName = String.Concat("d", d.ToString())
                              };
                Columns.Add(btc);
            }
            DataSource = new WorkingHoursForBinding(_template.WorkingHours).GetDataSource(); // thereby triggering DataSourceChanged
            DgridTemplate = pDataGridViewTemplate;            
            
        }        

        protected override void OnCellClick(DataGridViewCellEventArgs e)
        {
            if (!Program.Logged.CanEditTemplates)
            {
                return;
            }
            #region column 0 (AddAnotherRowString another)
            if (e.ColumnIndex == 0)
            {                
                Program.addempForm = new AddRowToTemplate();

                if (ShowDialogAt(
                    0,
                    0,
                    Program.addempForm,
                    10) == DialogResult.OK)
                {
                    if (DgridTemplate != null)                    
                        DgridTemplate.AddRow(Program.addempForm.Result);                    
                    // template.AddRow(Program.addempForm.result.position, Guid.NewGuid());                    
                }
                
                Program.addempForm.Dispose();
                return;
            }
            #endregion

            #region column 1-6 (Monday ... Sunday)
            if (e.ColumnIndex <= 0)
            {
                return;
            }
            Program.workinghrsForm = new WorkingHours(_template.WorkingHours, e.ColumnIndex - 1, e.ColumnIndex < 6);
                                
            switch (ShowDialogAt(
                e.ColumnIndex,
                0,
                Program.workinghrsForm,
                10))
            {
                case DialogResult.OK:
                    {
                        var result = Program.workinghrsForm._workingHours;
                        _template.SetWorkingHours(result, new[] { (Weekday)(e.ColumnIndex - 1) });
                        DataSource = new WorkingHoursForBinding(_template.WorkingHours).GetDataSource();
                        break;
                    }

                    // all five days
                case DialogResult.Yes:
                    {
                        var result = Program.workinghrsForm._workingHours;
                        _template.SetWorkingHours(result, new[]
                                                              {
                                                                  Weekday.Monday,
                                                                  Weekday.Tuesday,
                                                                  Weekday.Wednesday,
                                                                  Weekday.Thursday,
                                                                  Weekday.Friday
                                                              } );
                        DataSource = new WorkingHoursForBinding(_template.WorkingHours).GetDataSource();
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            Program.workinghrsForm.Dispose();
            #endregion
        }

        
        public static List<StaffRole> GetOptionsForAddAnother
        {
            get
            {
                var result = new List<StaffRole>();
                if (Settings.Default.ADDEmployeOnTemplate_StoreRunner)
                {
                    result.Add(new StaffRole(PositionExtensionMethods.StoreRunner));
                }
                if (Settings.Default.ADDEmployeeOnTemplate_StoreManager)
                {
                    result.Add(new StaffRole(Position.StoreManager));
                }
                if (Settings.Default.ADDEmployeeOnTemplate_AssistantManager)
                {
                    result.Add(new StaffRole(Position.AssistantManager));
                }
                if (Settings.Default.ADDEmployeeOnTemplate_Manager)
                {
                    result.Add(new StaffRole(PositionExtensionMethods.Manager));
                }
                if (Settings.Default.ADDEmployeeOnTemplate_StoreDeputy)
                {
                    result.Add(new StaffRole(Position.StoreDeputy));
                }
                if(Settings.Default.ADDEmployeeOnTemplate_Anyone)
                {
                    result.Add(new StaffRole((PositionExtensionMethods.Anyone)));
                }
                if (Settings.Default.ADDEmployeeOnTemplate_StoreAssistant)
                {
                    result.Add(new StaffRole(Position.StoreAssistant));
                }
                if (Settings.Default.ADDEmployeeOnTemplate_Assistant)
                {
                    result.Add(new StaffRole(PositionExtensionMethods.Assistant));
                }
                if (Settings.Default.ADDEmployeeOnTemplate_Trainee)
                {
                    result.Add(new StaffRole(Position.Trainee));
                }
                if (Settings.Default.ADDEmployeeOnTemplate_External)
                {
                    result.Add(new StaffRole(Position.External));
                }
                return result;
            }
        }
    }
}
